home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-03-13 | 4.7 KB | 253 lines | [TEXT/CWIE] |
- #include "SortPicts.h"
- #include <TextUtils.h>
- #include <Resources.h>
- #include <string.h>
-
- extern long gNumSortPictResources;
- extern MenuHandle gPicturesMenu;
- extern Boolean gUseCoplandTasks;
-
- Boolean SortPicts::PrepareGWorld( ConstStr255Param pictName)
- {
-
- if( !LoadSortPicture( pictName))
- return false;
-
- if( !MakeSortGWorld())
- {
- ReleaseSortPicture();
- return false;
- }
-
- ReleaseSortPicture();
-
- if( !AllocSortData())
- {
- DisposeGWorld( sortGWorld);
- return false;
- }
-
- MakeSortData();
- InitializeSortWindowRect( pictName);
-
- return true; // YES!! The everything worked out!
- }
-
- Boolean SortPicts::LoadSortPicture( ConstStr255Param pictName)
- {
- sortPict = (PicHandle)GetNamedResource( 'PICT', pictName);
-
- if( sortPict == nil)
- return false;
-
- sortRect = (*sortPict)->picFrame;
- pictWidth = sortRect.right - sortRect.left;
- pictHeight = sortRect.bottom - sortRect.top;
-
- SetRect( &sortRect, 0, 0, (short) pictWidth, (short) pictHeight);
- copyBitsRect = sortRect;
-
- return true;
- }
-
- void SortPicts::ReleaseSortPicture( void)
- {
- ReleaseResource( (Handle) sortPict);
-
- sortPict = nil;
- }
-
- Boolean SortPicts::MakeSortGWorld( void)
- {
- OSErr myOSErr;
- GWorldPtr tempSortGWorld;
- GDHandle oldGDevice;
- CGrafPtr oldGWorld;
- Rect tempSortRect;
-
- tempSortRect = sortRect;
-
- GetGWorld( &oldGWorld, &oldGDevice);
-
- myOSErr = NewGWorld( &tempSortGWorld, 8, &tempSortRect,
- (CTabHandle) 0, (GDHandle) 0, keepLocal);
-
- if( myOSErr != noErr)
- return false;
-
- sortGWorld = tempSortGWorld;
-
- SetGWorld( sortGWorld, (GDHandle)0);
- sortPixmap = GetGWorldPixMap( sortGWorld );
- LockPixels( sortPixmap);
-
-
- EraseRect( &sortRect);
- DrawPicture( sortPict, &sortRect);
-
- UnlockPixels( sortPixmap );
- SetGWorld( oldGWorld, oldGDevice);
-
- return true;
- }
-
-
- Boolean SortPicts::AllocSortData( void)
- {
- sortPixmap = GetGWorldPixMap( sortGWorld);
-
- pictRowBytes = (*sortPixmap)->rowBytes & 0x3FFF;
-
- N = pictWidth * pictHeight;
-
- sortHandle = (SortDataHandle) NewHandle( N * sizeof(long) );
-
- if( sortHandle == nil)
- return false;
-
- if ( gUseCoplandTasks )
- {
- HLockHi( (Handle)sortHandle );
- sortData = *sortHandle;
-
- // • Create a pixel buffer for the copland task. It doesn't really have
- // • access to the memory that's in the GWorld...
- //pixelBuffer = NewHandle( N );
- //HLockHi( (Handle)pixelBuffer );
- //pixelBufferData = *pixelBuffer;
- }
-
- return true;
- }
-
-
- void SortPicts::CopyFromSortData( void )
- {
- long horiz;
- long loop;
- SortPixel pixel;
-
- horiz = 0;
-
- UseSortData();
-
- for( loop = 0; loop < N; ++loop)
- {
- pixel = sortData[loop];
- sortPixels[horiz] = pixel;
- //pixelBufferData[loop] = pixel;
-
- if( ++horiz == pictWidth)
- {
- horiz = 0;
- sortPixels += pictRowBytes;
- }
- }
-
- UnuseSortData();
- }
-
-
- void SortPicts::ReleaseSortData( void)
- {
- DisposeHandle( (Handle) sortHandle );
- sortHandle = nil;
-
- //DisposeHandle( (Handle) pixelBuffer );
- //pixelBuffer = nil;
- //pixelBufferData = nil;
- }
-
-
- void SortPicts::MakeSortData( void)
- {
- long horiz;
- long loop;
- SortPixel pixel;
-
- UseSortData();
-
- horiz = 0;
-
- for( loop = 0; loop < N; ++loop)
- {
- pixel = sortPixels[horiz];
- sortData[loop] = ((SortData) pixel) | (loop << 8);
- //pixelBufferData[loop] = pixel;
-
- if( ++horiz == pictWidth)
- {
- horiz = 0;
- sortPixels += pictRowBytes;
- }
- }
-
- UnuseSortData();
- }
-
-
- void SortPicts::InitializeSortWindowRect( ConstStr255Param pictName)
- {
- Rect **windPictRectResource;
-
- gNumWindowsCreated++;
-
- if( (gPrefsRef > 0) &&
- ((windPictRectResource = (Rect **) GetNamedResource( 'RECT', pictName)) != NULL))
- {
- windPictRect = **windPictRectResource;
- }
- else
- {
- windPictRect = sortRect;
- windPictRect.top += kWindPictRectVoffset;
- windPictRect.bottom += kWindPictRectVoffset;
-
- OffsetRect( &windPictRect,
- (short) gNumWindowsCreated * 10, (short) gNumWindowsCreated * 10 + 30);
- }
- }
-
- void SortPicts::OpenDefaultSortWindows( void)
- {
- SortPicts *newWindow;
- Str255 defaultWindowName;
- short i;
-
- /* Open additional windows for each of the Preferences Rects out there */
- i = 2;
- do
- {
- GetIndString( defaultWindowName, kDefaultWindowResource, i);
- if( defaultWindowName[0] == 0)
- break;
-
- newWindow = new SortPicts( defaultWindowName);
-
- i ++;
- } while( true);
-
- GetIndString( defaultWindowName, kDefaultWindowResource, 1);
-
- if( defaultWindowName[0] != 0)
- {
- ::strcpy( (char *) sortPictsName, (char *) defaultWindowName);
- CreateWindow( kNormalWindow);
- }
- else
- {
- SelectRandomPicture();
- CreateWindow( kNormalWindow);
- }
- }
-
- void SortPicts::SelectRandomPicture( void)
- {
- long r;
- Str255 randomPictName;
-
- r = Random( gNumSortPictResources);
- GetMenuItemText( gPicturesMenu, (short) r, randomPictName);
- ::strcpy( (char *) sortPictsName, (char *) randomPictName);
- }
-